package bx.com.smartcashier.utils;


import com.alibaba.fastjson.TypeReference;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import bx.com.smartcashier.mode.GoodsDetailVO;


public class JsonUtils {


    /**
     * 将json 数组转换为Map 对象
     *
     * @param jsonString
     * @return
     */

    public static Map<String, Object> getMap(String jsonString)

    {

        JSONObject jsonObject;

        try

        {

            jsonObject = new JSONObject(jsonString);
            @SuppressWarnings("unchecked")

            Iterator<String> keyIter = jsonObject.keys();

            String key;

            Object value;

            Map<String, Object> valueMap = new HashMap<String, Object>();

            while (keyIter.hasNext())

            {

                key = (String) keyIter.next();

                value = jsonObject.get(key);

                valueMap.put(key, value);

            }

            return valueMap;

        } catch (JSONException e)

        {

            e.printStackTrace();

        }

        return null;

    }


    /**
     * 把json 转换为ArrayList 形式
     *
     * @return
     */

    public static List<Map<String, Object>> getList(String jsonString)

    {

        List<Map<String, Object>> list = null;

        try

        {

            JSONArray jsonArray = new JSONArray(jsonString);

            JSONObject jsonObject;

            list = new ArrayList<Map<String, Object>>();

            for (int i = 0; i < jsonArray.length(); i++)

            {

                jsonObject = jsonArray.getJSONObject(i);

                list.add(getMap(jsonObject.toString()));

            }

        } catch (Exception e)

        {

            e.printStackTrace();

        }

        return list;

    }

    /*
       将集合解析为json字符串
     */
    public static Object jsonEnclose(Object obj) {
        try {
            if (obj instanceof Map) {   //如果是Map则转换为JsonObject
                Map<String, Object> map = (Map<String, Object>) obj;
                Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
                JSONStringer jsonStringer = new JSONStringer().object();
                while (iterator.hasNext()) {
                    Map.Entry<String, Object> entry = iterator.next();
                    jsonStringer.key(entry.getKey()).value(jsonEnclose(entry.getValue()));
                }
                JSONObject jsonObject = new JSONObject(new JSONTokener(jsonStringer.endObject().toString()));
                return jsonObject;
            } else if (obj instanceof List) {  //如果是List则转换为JsonArray
                List<Object> list = (List<Object>) obj;
                JSONStringer jsonStringer = new JSONStringer().array();
                for (int i = 0; i < list.size(); i++) {
                    jsonStringer.value(jsonEnclose(list.get(i)));
                }
                JSONArray jsonArray = new JSONArray(new JSONTokener(jsonStringer.endArray().toString()));
                return jsonArray;
            } else {
                return obj;
            }
        } catch (Exception e) {
            // TODO: handle exception

            return e.getMessage();
        }
    }


    public static Object toObject(String jsonString, Class cls) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(jsonString, cls);
    }

    public static List<Object> toList(String jsonString, Class cls) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class,cls);
        return objectMapper.readValue(jsonString, javaType);
    }


}